﻿if (typeof (GoogleMaps) == "undefined") { GoogleMaps = {}; }

GoogleMaps = {
    _items: null,
    _itemsSelecteds: null,

    showMap: function () {
        if (Util.isGoogleAvailable()) {

            Util.showNotification("Loading...");

            var context = new SP.ClientContext.get_current();
            var web = context.get_web();

            var currentListId = SP.ListOperation.Selection.getSelectedList();
            var listGMaps = web.get_lists().getById(currentListId);
            this._itemsSelecteds = SP.ListOperation.Selection.getSelectedItems(context);
            var queryRede = SP.CamlQuery.createAllItemsQuery();
            this._items = listGMaps.getItems(queryRede);

            context.load(this._items);
            context.executeQueryAsync(Function.createDelegate(this, this.onGetSucess), Function.createDelegate(this, this.onFailed));

        }
        else {
            Util.showNotification("Cannot load GoogleMaps API. Please, verify your Internet Connection and try again.");
        }
    },

    criarObjGMaps: function () {
        var htmlDIV = document.createElement("div");
        htmlDIV.setAttribute("id", "map_canvas");
        htmlDIV.setAttribute("style", "position: relative; width: 100%; height: 100%");
        return htmlDIV;
    },

    renderMap: function (itemArray) {
        try {
            if (itemArray != null && itemArray.length > 0) {
                Util.showModal('Google Maps Viewer', null, this.criarObjGMaps(), 560, 740, true);

                var latlng = new google.maps.LatLng(itemArray[0].Latitude, itemArray[0].Longitude);
                this.genericOptions = {
                    zoom: 6,
                    center: latlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                var gmap = new google.maps.Map(document.getElementById("map_canvas"), this.genericOptions);


                this.setMarkers(itemArray, gmap);
            }
        }
        catch (e) { Util.showError(e.message); }
    },

    setMarkers: function (array, map) {
        try {
            var contentString = "";
            var infoWindow = null;

            infoWindow = new google.maps.InfoWindow({
                content: contentString
            });

            for (var i = 0; i < array.length; i++) {
                var _marker = array[i];
                var myLatLng = new google.maps.LatLng(_marker.Latitude, _marker.Longitude);

                var mk = new google.maps.Marker({
                    position: myLatLng,
                    map: map,
                    title: _marker.Title
                });
            }
        }
        catch (e) { Util.showError(e.message) }
    },

    getItemData: function (lookup) {

        _link = null;
        var array = this._items.getEnumerator();

        while (array.moveNext()) {
            var current = array.get_current();
            if (current.get_id() == lookup.id)

                if (current.get_item('dotdevLongitude0') != null && current.get_item('dotdevLatitude0') != null) {
                    _link = {
                        Title: current.get_item('Title'),
                        Longitude: current.get_item('dotdevLongitude0'),
                        Latitude: current.get_item('dotdevLatitude0')
                    }
                    break;
                }
        }

        return _link;
    },

    onGetSucess: function (sender, args) {
        try {
            counter = 0;
            var _itemArray = new Array();
            for (var i = 0; i < this._itemsSelecteds.length; i++) {
                var _i = this.getItemData(this._itemsSelecteds[i]);
                if (_i != null) {
                    _itemArray[i] = _i;
                }
                else {
                    _itemArray = null;
                    break;
                }
            }
            GoogleMaps.renderMap(_itemArray);

        }
        catch (e) {
            Util.showError(e.message);
        }
    },

    onFailed: function (sender, args) {
        Util.showNotification(args);
    }

}